package com.ruoyi.yf.tool;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class TextSeparation {

    public static void main(String[] args) throws IOException {

//        readText("C:\\Users\\Dell\\Desktop\\斗罗大陆.txt", "C:\\Users\\Dell\\Desktop\\", "斗罗大陆", 2000001);
//        readLine("C:\\Users\\Dell\\Desktop\\斗罗大陆.txt", "C:\\Users\\Dell\\Desktop\\", "斗罗大陆", 20000);


        System.out.println(isUTF8("中文"));
    }


    public static boolean isUTF8(String key) {
        try {
            key.getBytes("utf-8");
            return true;
        } catch (UnsupportedEncodingException e) {
            return false;
        }
    }

    /**
     * @param filePath
     * @param outFilepath
     * @param fileName
     * @param size
     *         行
     *         e8ed6d68-398b-4458-b169-2d68bf754354
     */
    public static void readLine(String filePath, String outFilepath, String fileName, int size) {
        FileReader fileReader = new FileReader(filePath, "utf-8");
        FileWriter writer = null;
        List<String> strings = fileReader.readLines();
        int no = 0;
        String str = "";
        System.out.println(strings.size());
        for (int i = 0; i < strings.size(); i++) {
            if (i == 0) {
                writer = new FileWriter(outFilepath + fileName + "1.txt", "utf-8");
            } else if (no == size) {
                writer = new FileWriter(outFilepath + fileName + i + ".txt", "utf-8");
                no = 0;
            }
            writer.write(strings.get(i) + "\r\n", true);
            no++;
        }

    }

/**
 *
 * <p/>
 *
 * <p/>
 *
  * @param	filePath
 * @param	outFilepath
 * @param	fileName
 * @param	size
 * @return void
 *
 * @throws
 * @author  XiaoYi...
 * @date    2021/1/28 5:12 下午
 * 60980260-8c97-49aa-90fe-10643a6f85a2
 */
    public static void readText(String filePath, String outFilepath, String fileName, int size) {
        FileReader fileReader = new FileReader(filePath, "utf-8");
        byte[] result = fileReader.readBytes();

        System.out.println(result.length);


        System.out.println(result.length / size);
        int no = result.length / size + 1;//文件总次数
        int start = 0;//文章开始位
        for (int i = 1; i <= no; i++) {
            FileWriter writer = new FileWriter(outFilepath + fileName + i + ".txt", "utf-8");
            int len = size;
            if (i == no)
                len = result.length - start;
            System.out.println(result.length + ";" + i + "==" + no + "<>" + start + "<>" + len);
            writer.write(result, start, len);
            start += size;
        }


    }
}
